﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XQ.WeChatOfficialAccount.DAL.Model;

namespace XQ.WeChatOfficialAccount.DAL.DAL
{
    public class RoleMenusDal
    {
        private WeChatOfficialAccountEntities ce;


        /// <summary>
        /// 检查角色与菜单关系
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="menuId">菜单ID</param>
        /// <returns></returns>
        public bool CheckRoleMenu(int roleId, int menuId)
        {
            using (ce = new WeChatOfficialAccountEntities())
            {
                return ce.Sys_Role_Menu_Btns.Where(n => n.RoleId == roleId && n.MenuID == menuId).ToList().Count > 0
                    ? true
                    : false;
            }
        }

        /// <summary>
        /// 获了菜单按钮
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="menuId">菜单ID</param>
        /// <returns></returns>
        public List<RoleMenuModel> GetMenuButtons(int roleId, int menuId)
        {
            using (ce = new WeChatOfficialAccountEntities())
            {
                var result = new List<RoleMenuModel>();
                List<Sys_Menu_Buttons> buttonses = ce.Sys_Menu_Buttons.Where(p => p.NavId == menuId).ToList();
                foreach (var item in buttonses)
                {
                    var roleMenul = new RoleMenuModel
                    {
                        ButtonId = item.ButtonId,
                        ID = item.ID,
                        NavId = item.NavId,
                        RoleMenuFlag = CheckRoleMenu(roleId, item.NavId == null ? 0 : (int)item.NavId),
                        Sortnum = item.Sortnum,
                        ButtonName = GetButtonName((int)item.ButtonId)
                    };
                    result.Add(roleMenul);
                }
                return result;
            }
        }

        /// <summary>
        /// 根据角色ID获取所有对应的菜单权限
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns></returns>
        public List<Sys_Role_Menu_Btns> GetMenuButtonsByRoleID(int roleId)
        {
            using (ce = new WeChatOfficialAccountEntities())
            {
                List<Sys_Role_Menu_Btns> list = ce.Sys_Role_Menu_Btns.Where(rmbs => rmbs.RoleId == roleId).ToList();
                return list;
            }
        }

        /// <summary>
        /// 根据角色ID获取菜单ID数组
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="isHalfSelect"></param>
        /// <returns></returns>
        public List<int> GetMenuIDsByRoleID(int roleId, bool isHalfSelect=false)
        {
            using (ce = new WeChatOfficialAccountEntities())
            {
                List<int> menuIDs;
                if (!isHalfSelect)
                {
                    menuIDs = ce.Sys_Role_Menu_Btns.Where(rmbs => rmbs.RoleId == roleId && (rmbs.IsHalfSelect == false || rmbs.IsHalfSelect == null)).Select(rmb => (int)rmb.MenuID).ToList();
                }
                else {
                    menuIDs = ce.Sys_Role_Menu_Btns.Where(rmbs => rmbs.RoleId == roleId && rmbs.IsHalfSelect == isHalfSelect).Select(rmb => (int)rmb.MenuID).ToList();
                }
                return menuIDs;
            }
        }

        /// <summary>
        /// 获取按键名称
        /// </summary>
        /// <param name="btnId">按钮ID</param>
        /// <returns></returns>
        private string GetButtonName(int btnId)
        {
            using (ce = new WeChatOfficialAccountEntities())
            {
                Sys_Buttons button = ce.Sys_Buttons.FirstOrDefault(n => n.ID == btnId);
                if (button != null)
                {
                    return button.ButtonText;
                }

                return string.Empty;
            }
        }

        /// <summary>
        /// 获取角色的页面按钮
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="menuId">菜单ID</param>
        /// <param name="buttonId"></param>
        /// <returns></returns>
        public bool GetRoleButton(int roleId, int menuId, int buttonId)
        {
            using (ce = new WeChatOfficialAccountEntities())
            {
                var count = ce.Sys_Role_Menu_Btns.Where(n => n.MenuID == menuId && n.RoleId == roleId && n.BtnId == buttonId).ToList().Count;
                return count > 0 ? true : false;

            }
        }

        /// <summary>
        /// 保存角色权限分配
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <param name="menuId">菜单ID</param>
        /// <param name="btnId">菜单ID</param>
        /// <param name="flag">标记</param>
        public void SaveRights(int roleId, int menuId, int btnId, bool flag)
        {
            //添加
            if (flag)
            {
                using (ce = new WeChatOfficialAccountEntities())
                {
                    string sql = @"if not exists (select * from Sys_Role_Menu_Btns where RoleId=@roleId and MenuID=@menuId and BtnId=@btnId)
  insert into Sys_Role_Menu_Btns([RoleId] ,[MenuID],[BtnId]) values(@roleId,@menuId,@btnId)";
                    object[] par =
                    {
                        new SqlParameter("@roleId",roleId),
                        new SqlParameter("@menuId",menuId),
                        new SqlParameter("@btnId",btnId)
                    
                    };
                    ce.Database.ExecuteSqlCommand(sql, par);
                }
            }
            //删除
            else
            {
                using (ce = new WeChatOfficialAccountEntities())
                {
                    var srmb = ce.Sys_Role_Menu_Btns.First(n => n.RoleId == roleId && n.MenuID == menuId && n.BtnId == btnId);
                    if (srmb != null)
                        ce.Sys_Role_Menu_Btns.Remove(srmb);
                    ce.SaveChanges();
                }
            }

        }

        /// <summary>
        /// 保存角色权限分配
        /// </summary>
        public bool SaveRights(int roleId, List<int> MenuIDs, List<int> halfMenuIDs)
        {
            if (roleId > 0)
            {
                using (ce = new WeChatOfficialAccountEntities())
                {
                    //删除当前角色的所有权限
                    var srmb = ce.Sys_Role_Menu_Btns.Where(n => n.RoleId == roleId);
                    if (srmb != null)
                    {
                        ce.Sys_Role_Menu_Btns.RemoveRange(srmb);
                    }
                    ce.SaveChanges();

                    //重新添加选择的权限(全选菜单)
                    if (MenuIDs != null && MenuIDs.Count > 0)
                    {
                        Sys_Role_Menu_Btns rmb;
                        foreach (int menuId in MenuIDs)
                        {
                            rmb = new Sys_Role_Menu_Btns
                            {
                                MenuID = menuId,
                                RoleId = roleId,
                                BtnId = 0,
                                IsHalfSelect = false
                            };
                            ce.Sys_Role_Menu_Btns.Add(rmb);
                        }
                    }
                    //重新添加选择的权限(半选菜单)
                    if (halfMenuIDs != null && halfMenuIDs.Count > 0)
                    {
                        Sys_Role_Menu_Btns halfSelectRMB;
                        foreach (int menuId in halfMenuIDs)
                        {
                            halfSelectRMB = new Sys_Role_Menu_Btns
                            {
                                MenuID = menuId,
                                RoleId = roleId,
                                BtnId = 0,
                                IsHalfSelect = true
                            };
                            ce.Sys_Role_Menu_Btns.Add(halfSelectRMB);
                        }
                    }
                    return ce.SaveChanges() > 0;
                }
            }
            return false;
        }

        public void DleteRights(int roleId, int menuId)
        {

            using (ce=new WeChatOfficialAccountEntities())
            {
                var srmb = ce.Sys_Role_Menu_Btns.Where(n => n.RoleId == roleId && n.MenuID == menuId );
                if (srmb != null) { 
                    foreach (var sr in srmb)
                    {
                        ce.Sys_Role_Menu_Btns.Remove(sr);
                    }
                }
                   
                ce.SaveChanges();
            }
        }

        public List<int> GetMenusByUser(int userid)
        {
            using (ce = new WeChatOfficialAccountEntities())
            {
                int? roleIdTemp = 0;
                Sys_User_Roles urModel = ce.Sys_User_Roles.FirstOrDefault(ur => ur.UserID == userid);
                if (urModel != null)
                {
                    roleIdTemp = urModel.RoleID;
                }
                int roleId = roleIdTemp == null ? 0 : (int)roleIdTemp;

                return ce.Sys_Role_Menu_Btns.Where(n => n.RoleId == roleId).OrderBy(p => p.MenuID).Select(p => p.MenuID.Value).ToList();
            }
        }

    }


}
